# -*- coding:UTF-8 -*-

# author:user
# contact: test@test.com
# datetime:2021/11/22 12:03
# software: PyCharm

"""
文件说明：
    
"""
import asyncio

from rasa.shared.nlu.training_data.loading import load_data
from rasa.shared.importers.importer import TrainingDataImporter
from rasa.nlu.config import RasaNLUModelConfig
from rasa.nlu.model import Trainer
from rasa.shared.utils.io import read_config_file
from rasa.nlu import config
from rasa.core.agent import Agent
from rasa.core.policies.memoization import MemoizationPolicy
from rasa.core.interpreter import RasaNLUInterpreter
from rasa.core.policies.ted_policy import TEDPolicy
from rasa.core.policies.rule_policy import RulePolicy
from rasa.core.channels.console import CmdlineInput
## 在线学习
async def train_online(input_channel=CmdlineInput(),
                 interpreter=RasaNLUInterpreter("models/nlu_20211122-111458"),
                 domain_file="./domain.yml",
                 training_data_file="data/story.yml"):
    agent = Agent(domain_file,
                  policies=[MemoizationPolicy(), TEDPolicy(), RulePolicy()],
                  interpreter=interpreter)
    training_data = await agent.load_data(training_data_file)
    agent.train_online(training_data,
                       input_channel=input_channel,
                       max_history=2,
                       batch_size=50,
                       epochs=200,
                       max_training_samples=300)

## 学习模型策略
async def train_dialogue(domain_file="domain.yml",
                   model_path="models/dialogue",
                   training_data_file="./data/stories.yml"):
    agent = Agent(domain_file,
                  policies=[MemoizationPolicy(), TEDPolicy(), RulePolicy()])
    training_data = await agent.load_data(training_data_file)
    agent.train(
        training_data,
        max_history=3,
        epochs=100,
        batch_size=50,
        augmentation_factor=50,
        validation_split=0.2
    )

    agent.persist(model_path)


loop = asyncio.get_event_loop()
loop.run_until_complete(train_dialogue())
#
# if __name__ == '__main__':
#     train_online()
